package com.gimi.cloud.jimi.client.common.springUtil;

import com.gimi.cloud.jimi.commonService.common.JmInitService;
import com.gimi.cloud.jimi.commonService.util.InitGlobal;
import com.gimi.cloud.jimi.dto.sys.SysConfigDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;


/**
 * 初始化配置
 * Created by shazhiwei on 2017/9/6.
 *
 */
@Component
public class InitConfig implements CommandLineRunner {

	private static Logger logger = LoggerFactory.getLogger(InitConfig.class);
	
	//public final static Map<String, SysConfigEntity> config = new Hashtable<>();
	//配置状态为开启

	private ServletContext context;
	private JmInitService jmInitService;
	@Autowired
	public InitConfig(ServletContext context, JmInitService jmInitService) {
		this.context = context;
		this.jmInitService = jmInitService;
	}
	@Override
	public void run(String... arg0) throws Exception {

		/*Example example=new Example(SysConfigEntity.class);
		example.createCriteria().andCondition("ba_remark=","inintRedis");
		List<SysConfigEntity> sysConfigEntityList=sysConfigDao.selectByExample(example);
		for (SysConfigEntity sysConfigEntity:sysConfigEntityList) {
			redisService.set(RedisKeyEnum.JIMI_CONFIG_PRE.getKey()+sysConfigEntity.getCfKey(),sysConfigEntity.getCfValue());
		}

		//每次启动，重新加载redis配置
		List<SysConfigDTO> sysConfigDTOS = sysConfigDao.getConfigByCfkey("exemption_service_name");
		if(sysConfigDTOS != null && sysConfigDTOS.size()>0){
			SysConfigDTO dto = sysConfigDTOS.get(0);
			String mysqlData = dto.getCfValue();
			Map<String, String> dataMap = JsonUtil.jsonToMap(mysqlData);
			if(dataMap != null && dataMap.size() > 0){
				redisService.set(RedisKeyEnum.JIMI_CONFIG_PRE.getKey() + "ExemptionServiceName",mysqlData);
			}
		}*/


		jmInitService.initSysConfig();//加载sys_config数据
		jmInitService.initOssClient();//初始化oss客户端
		//加载global
		InitGlobal.init(context);
	}


}
